/**
 * Copyright (c) 2013, BigBeard Team, Inc. All rights reserved. 
 */
package com.xtgg.util;

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.net.InetSocketAddress;
import java.util.List;

public class NetworkUtil {
	/**
	 * The invalid APN
	 */
	public static final String INVALID_ACCESS_POINT = "None";
	public static final String Network_2G = "2G";
	public static final String Network_3G = "3G";
	public static final String Network_WIFI = "WIFI";

	public static final int NETWORK_TYPE_LTE = 13;
	public static final int NETWORK_TYPE_IDEN = 11;
	public static final int NETWORK_TYPE_HSUPA = 9;
	public static final int NETWORK_TYPE_HSPAP = 15;
	public static final int NETWORK_TYPE_HSPA = 10;
	public static final int NETWORK_TYPE_HSDPA = 8;
	public static final int NETWORK_TYPE_EVDO_B = 12;
	public static final int NETWORK_TYPE_EHRPD = 14;

	/**
	 * 获取网络接入点
	 * 
	 * @return {@link #INVALID_ACCESS_POINT} or {@link #Network_WIFI} or
	 *         "cmnet","cmwap"诸如此类
	 * @author yinb
	 */
	public static String getAccessPoint(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
		if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
			return INVALID_ACCESS_POINT;
		}
		String networkType = info.getTypeName();
		if (networkType != null && !networkType.equalsIgnoreCase("WIFI")) {
			String mobiName = info.getExtraInfo();
			if (mobiName != null && !mobiName.equals("")) {
				networkType = mobiName;
			}
		}
		return networkType;
	}

	/**
	 * 判断移动网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isMobileNetworkActive(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = mConnectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
			return activeNetInfo.isAvailable();
		}
		return false;
	}

	/**
	 * 判断wifi是否可用
	 * 
	 * @param context
	 * @return
	 * @author yinb
	 */
	public boolean isWifiActive(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWiFiNetworkInfo = mConnectivityManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWiFiNetworkInfo != null) {
			return mWiFiNetworkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * Gps是否打开
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isGpsEnabled(Context context) {
		LocationManager locationManager = ((LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE));
		List<String> accessibleProviders = locationManager.getProviders(true);
		return accessibleProviders != null && accessibleProviders.size() > 0;
	}

	/**
	 * 判断当前网络是不3G网络
	 * 
	 * @param context
	 * @return
	 * @author yinb
	 */
	public static boolean is3G(Context context) {
		if (Network_3G.equals(getNetworkType(context))) {
			return true;
		}
		return false;
	}

	/**
	 * 网络是否可用
	 * 
	 * @param activity
	 * @return 如果有可用连接则返回true
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (mConnectivityManager == null) {
		} else {
			NetworkInfo[] info = mConnectivityManager.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 获取当前网络类型
	 * 
	 * @return {@link #INVALID_ACCESS_POINT} or {@link #Network_2G} or
	 *         {@link #Network_3G} or {@link #Network_WIFI}
	 * @author yinb
	 */
	public static String getNetworkType(Context context) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
		if (info == null || info.getState() != NetworkInfo.State.CONNECTED) {
			return INVALID_ACCESS_POINT;
		}

		int type = info.getType();
		TelephonyManager mTelephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		int subtype = mTelephonyManager.getNetworkType();

		return getMobileNetworkType(type, subtype);
	}

	private static String getMobileNetworkType(int type, int subtype) {
		if (type == ConnectivityManager.TYPE_WIFI) {
			return Network_WIFI;
		} else if (type == ConnectivityManager.TYPE_MOBILE) {
			switch (subtype) {

			case TelephonyManager.NETWORK_TYPE_CDMA:
			case TelephonyManager.NETWORK_TYPE_EVDO_0:
			case TelephonyManager.NETWORK_TYPE_EVDO_A:
			case NETWORK_TYPE_LTE:
			case NETWORK_TYPE_HSUPA:
			case NETWORK_TYPE_HSPAP:
			case NETWORK_TYPE_HSPA:
			case NETWORK_TYPE_HSDPA:
			case NETWORK_TYPE_EVDO_B:
			case NETWORK_TYPE_EHRPD:
				return Network_3G;

			case TelephonyManager.NETWORK_TYPE_1xRTT:
			case TelephonyManager.NETWORK_TYPE_EDGE:
			case TelephonyManager.NETWORK_TYPE_GPRS:
			case TelephonyManager.NETWORK_TYPE_UMTS:
			case TelephonyManager.NETWORK_TYPE_UNKNOWN:
			case NETWORK_TYPE_IDEN:
			default:
				return Network_2G;
			}
		}
		return Network_2G;
	}
}